<!-- 雷达图 -->
<template>
  <div>
    <h2>云端报警风险</h2>
    <div ref="target" class="chart2"></div>
  </div>
</template>
<script setup>
import * as echarts from 'echarts'
import { defineProps, onMounted, ref, watch } from 'vue'

const props = defineProps({
  data: {
    typeof: Object,
    required: true,
  }
})

// 1. 初始化 echarts 实例
let mChart = null
const target = ref(null)

onMounted(() => {
  mChart = echarts.init(target.value)
  renderChart()
})

// 2. 构建 option 配置对象 
const renderChart = () => {
  const options = {
    // 雷达图的坐标系配置
    radar: {
      name: {
        textStyle: {
          color: '#05d5ff',
          fontSize: 14
        }
      },
      shape: 'polygon',
      center: ['50%', '50%'],
      radius: '80%',
      startAngle: 120,
      axisLine: {
        lineStyle: {
          color: 'rgba(211, 253, 250, 0.8)'
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          width: 1,
          color: 'rgba(5,213,255,.8)'
        }
      },
      indicator: props.data.risks.map(item => ({
        name: item.name,
        max: 100
      })),
      splitArea: {
        show: false
      }
    },
    // 极坐标系配置
    polar: {
      center: ['50%', '50%'],
      radius: '0%'
    },
    // 角度轴
    angleAxis: {
      min: 0,
      interval: 5,
      clockwise: false
    },
    // 半径轴
    radiusAxis: {
      min: 0,
      interval: 20,
      splitLine: {
        show: false
      }
    },
    // 核心配置 
    series: {
      type: 'radar',
      symbol: 'circle',
      symbolSize: 10,
      itemStyle: {
        normal: {
          color: '#05d5ff',
          opacity: 0.5
        }
      },
      areaStyle: {
        normal: {
          color: '#05d5ff',
          opacity: 0.5
        }
      },
      lineStyle: {
        width: 2,
        color: '#05d5ff',
      },
      label: {
        normal: {
          show: true,
          color: '#fff',
        }
      },
      data: [props.data.risks.map(item => item.value)]
    }
  }
  // 3. 通过 实例.setOptions(option)
  mChart.setOption(options)
  // 让echarts根据浏览器大小改变动态该改变
  window.onresize = () => {//自适应大小
    mChart.resize()
  }
}

watch(() => props.data, renderChart)
</script>
<style lang="less" scoped>
h2 {
  height: .5rem;
  line-height: .5rem;
  text-align: center;
  // background-color: pink;
}

.chart2 {
  width: 100%;
  height: 3.75rem;
}
</style>